package com.css.bjdt_report.validation;

import java.util.ArrayList;
import java.util.List;

import com.css.bjdt_report.cell.entity.Cell;
import com.css.bjdt_report.executive.util.ExpParser;
import com.css.bjdt_report.executive.entity.Exp;
import com.css.bjdt_report.util.ReportConstant;

/**
 * 
 * @author qiushi
 *
 */
public class ValidationExpParser extends ExpParser {
	
	private static ValidationExpParser instance;
	
	private ValidationExpParser(){}
	
	public static ValidationExpParser getInstance(){
		if(instance == null){
			instance = new ValidationExpParser();
		}
		return instance;
	}

	@Override
	public void parseExp(Cell cell) {
		handleRowExp(cell);
		handleColExp(cell);
	}

	@Override
	public void handleResult(Cell cell, Exp exp) {
		if(cell.getCalcExp() != null){
			List<Exp> list = cell.getValidationExps();
			if(list == null){
				list = new ArrayList<Exp>();
				cell.setValidationExps(list);
			}
			String oldExp = cell.getOldExp();
			String[] array = oldExp.split(ReportConstant.OPERATOR_RELA);
			StringBuilder sb = new StringBuilder();
			sb.append("[").append(cell.getRelRowNum()).append(ReportConstant.SPLIT_OPERATOR).append(cell.getRelColNum()).append("]");				
			String validationExp = oldExp.replaceFirst(array[0], sb.toString()).replace(array[1], cell.getCalcExp());
			Exp e = new Exp();
			e.setFormulaType(exp.getFormulaType());
			e.setCellExp(validationExp);	
			e.setFormula(oldExp);
			e.setFormulaShow(exp.getFormulaShow());
			list.add(e);						
		}
	}
	
}
